package com.jeecg.modules.jmreport.test;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class HikariCpExample {
    // 数据库连接池配置
    private static HikariDataSource dataSource;

    static {
/*        url: jdbc:mysql://${MYSQL-HOST:43.138.154.67}:${MYSQL-PORT:3306}/${MYSQL-DB:jimureport}?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai
        username: weir
        password: weir336393QQ!*/
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl("jdbc:mysql://43.138.154.67:3306/jimureport?useSSL=false&serverTimezone=UTC&characterEncoding=utf8");
        config.setUsername("weir");
        config.setPassword("weir336393QQ!");
        config.setDriverClassName("com.mysql.cj.jdbc.Driver");

        // HikariCP特有配置
        config.setPoolName("HikariPool-1");
        config.setMaximumPoolSize(10);         // 最大连接数
        config.setMinimumIdle(5);              // 最小空闲连接数
        config.setIdleTimeout(30000);          // 空闲连接超时时间(毫秒)
        config.setMaxLifetime(1800000);        // 连接最大生命周期(毫秒)
        config.setConnectionTimeout(30000);    // 连接超时时间(毫秒)

        dataSource = new HikariDataSource(config);
    }

    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }

    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            // 从连接池获取连接
            connection = getConnection();
            System.out.println("从连接池获取连接成功");

            // 使用PreparedStatement防止SQL注入
            String querySql = "SELECT * FROM user WHERE department_id = ?";
            preparedStatement = connection.prepareStatement(querySql);
            preparedStatement.setInt(1, 101);  // 设置参数

            // 执行查询
            resultSet = preparedStatement.executeQuery();

            // 处理结果
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String username = resultSet.getString("username");
                System.out.println("ID: " + id + ", Username: " + username);
            }

            // 插入数据示例
            String insertSql = "INSERT INTO user (username, email, department_id) VALUES (?, ?, ?)";
            preparedStatement = connection.prepareStatement(insertSql);
            preparedStatement.setString(1, "newuser");
            preparedStatement.setString(2, "newuser@example.com");
            preparedStatement.setInt(3, 102);
            int insertCount = preparedStatement.executeUpdate();
            System.out.println("插入了 " + insertCount + " 条记录");

        } catch (SQLException e) {
            System.err.println("数据库操作失败: " + e.getMessage());
        } finally {
            // 关闭资源
            try {
                if (resultSet != null) resultSet.close();
                if (preparedStatement != null) preparedStatement.close();
                if (connection != null) connection.close(); // 实际上是将连接归还连接池
            } catch (SQLException e) {
                System.err.println("资源关闭失败: " + e.getMessage());
            }
        }
    }
}